package com.viewpager2.teach.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;


import com.viewpager2.teach.R;

import java.util.List;

/**
 * Creator:Yx
 * Time:2020/8/5 9:35
 * Description:ViewPager2中横纵向RecyclerView -  Adapter
 */
public class ViewPager2OneAdapter extends RecyclerView.Adapter<ViewPager2OneAdapter.ViewHolder> {

    private List<String> mLists;
    private LayoutInflater mInflater;
    private ViewPager2 viewPager2;

    private int[] colorArray = new int[]{android.R.color.black, android.R.color.holo_blue_dark, android.R.color.holo_green_dark, android.R.color.holo_red_dark};

    public ViewPager2OneAdapter(Context context, ViewPager2 viewpager2) {
        this.mInflater = LayoutInflater.from(context);
        this.viewPager2 = viewpager2;
    }

    public ViewPager2OneAdapter(Context context, List<String> list, ViewPager2 viewpager2) {
        this.mInflater = LayoutInflater.from(context);
        this.mLists = list;
        this.viewPager2 = viewpager2;
    }

    public void setViewPager2Data(List<String> list) {
        this.mLists = list;
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return mLists != null ? mLists.size() : 0;
    }

    @NonNull
    @Override
    public ViewPager2OneAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_viewpager2_recyclerview, parent, false);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewPager2OneAdapter.ViewHolder holder, int position) {
        String animal = mLists.get(position);
        holder.myTextView.setText(animal);
        holder.relativeLayout.setBackgroundResource(colorArray[position]);

    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView myTextView;
        RelativeLayout relativeLayout;

        ViewHolder(View itemView) {
            super(itemView);
            myTextView = itemView.findViewById(R.id.tvTitle);
            relativeLayout = itemView.findViewById(R.id.container);
        }
    }
}
